<?php
namespace app\service;

use think\Db; 

/**
 * 作者服务层
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class WarehouseService {

    /**
     * 获取列表
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-08-29
     * @desc    description
     * @param   array           $params [输入参数: where, field, is_photo]
     */
    public static function WarehouseList($params = [], $flat = 0) {

        $where = empty($params['where']) ? [] : $params['where'];

        $order_by = empty($params['order_by']) ? 'id desc' : trim($params['order_by']);

        $m = isset($params['m']) ? intval($params['m']) : 0;
        $n = isset($params['n']) ? intval($params['n']) : 10;
        $data = Db::name('Warehouse')->alias('w')->join(['__ADMIN__' => 'a'], 'a.id=w.aid')->field('w.*,a.username')->where($where)->order($order_by)->limit($m, $n)->select();

        return DataReturn('处理成功', 0, $data);
    }

    /**
     * 获取书单总数
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-07
     * @desc    description
     * @param   [array]           $where [条件]
     */
    public static function WarehouseTotal($where = []) {
        return (int) Db::name('Warehouse')->where($where)->count();
    }

     /**
     * [ 仓库列表]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     */
    public static function getAdminWarehouse($is_quanbu = false) {
        $where = [];
       if(!$is_quanbu)
       {
              $admin =  session("admin");
          
              //角色等于2表示为仓库管理员账号
              if($admin['role_id'] == 2)
              {
                  $where[] = ['aid','=',$admin['id']];           
              }
              else if($admin['role_id'] == 3){
                  //角色等于3表示为录书员账号
                   
                  $where[] = $admin['warehouse_id'] == 0 ? [] : ['id','=',$admin['warehouse_id']];
                       
              }  
       }
       $data  = Db::name('Warehouse')->where($where)->field("warehouse_name,id")->select();
         
         return   $data;
    }
    
    /**
     * 仓库保存
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2018-12-10T01:02:11+0800
     * @param    [array]          $params [输入参数]
     */
    public static function WarehouseSave($params = []) {

        // 请求参数
        $p = [
            [
                'checked_type' => 'empty',
                'key_name' => 'warehouse_name',
                'error_msg' => '仓库名称不能为空',
            ],
             [
                'checked_type' => 'length',
                'key_name' => 'code',
                'checked_data' => '4,4',
                'error_msg' => '仓库代码必须4位字符',
            ],
            [
                'checked_type' => 'length',
                'key_name' => 'warehouse_name',
                'checked_data' => '2,60',
                'error_msg' => '仓库名称格式 2~60 个字符',
            ],
             [
                'checked_type' => 'length',
                'key_name' => 'address',
                'checked_data' => '2,100',
                'error_msg' => '仓库地址格式 2~100 个字符',
            ],
             [
                'checked_type' => 'length',
                'key_name' => 'describe',
                'checked_data' => '2,100',
                'error_msg' => '仓库描述格式 2~200 个字符',
            ],
              [
                'checked_type' => 'empty',
                'key_name' => 'aid',
                'error_msg' => '请选择仓库管理员',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if ($ret !== true) {
            return DataReturn($ret, -1);
        }
      
    
        // 基础数据
        $data = [
            'warehouse_name' => $params['warehouse_name'],
             'code' => $params['code'],
             'aid' => $params['aid'],
            'province_name' => $params['province_name'],
            'city_name' => $params['city_name'],
            'exparea_name' => $params['exparea_name'],
            
             'phone'   => $params['phone'],
             'address'   => $params['address'],
             'describe'   => $params['describe'],
             'size'   => $params['size'],
        ];
        // 添加/编辑
        if (empty($params['id'])) {
            $data['add_time'] = time();
            $goods_id = Db::name('Warehouse')->insertGetId($data);
        } else {
           
            if (Db::name('Warehouse')->where(['id' => intval($params['id'])])->update($data)) {
                $goods_id = $params['id'];
            }
        }
     
        return DataReturn('操作成功', 0);
    }
     
 
 
    
    /**
     * 删除
  
     * @version  1.0.0
     * @datetime 2018-12-07T00:24:14+0800
     * @param    [array]          $params [输入参数]
     */
    public static function warehouseDelete($params = []) {
        // 参数是否有误
        if (empty($params['id'])) {
            return DataReturn('仓库id有误', -1);
        }

       
        // 删除商品
        if (Db::name('Warehouse')->delete(intval($params['id']))) {
            
            return DataReturn('删除成功', 0);
        }
 
        return DataReturn('删除失败', -100);
    }
     

}

?>